import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { MenuItem, TreeNode, AutoComplete, Message } from 'primeng/primeng';
import { flyIn } from '../../common/animation/common.animation';
import { RoleService } from './service/role.service';

@Component({
  templateUrl: './role.component.html',
  styleUrls: ['./role.component.scss'],
  providers: [RoleService],
  animations: [flyIn]
})
export class RoleComponent implements OnInit {
  loading: boolean = true;
  unitRoles: TreeNode[];
  selectedNode: TreeNode;
  suggestions: any[];
  suggestion: any;
  constructor(
    private roleService: RoleService,
    private route: ActivatedRoute,
    private router: Router) { }

  ngOnInit() {
    /** 初始化树节点 */
    this.roleService.getUnitRoleTree(0, true).subscribe(data => {
      this.loading = false;
      this.unitRoles = data;
      if (this.unitRoles[0]) {
        this.selectedNode = this.unitRoles[0];
        this.nodeSelect(null);
      }
    });

    //监听子组件中添加或删除事件，刷新树控件列表
    this.roleService.routeMsgPush$.subscribe(data => this.recursionRefreshTreeNode(this.unitRoles, data));
  }
  /** 树节点展开事件 */
  unitNodeExpand(event) {
    if (event.node && event.node.children.length == 0) {
      let unitId = +event.node.id;
      this.roleService.getUnitRoleTree(unitId, false).subscribe(nodes => event.node.children = nodes);
    }
  }

  /**机构/岗位查询事件 */
  search(event) {
    if (!event.query) return;
    this.roleService.searchUnitRoleByName(event.query).subscribe(data => {
      this.suggestions = data
    });
  }
  /** 选择节点 */
  selectSuggestion() {
    this.selectedNode = null;
    this.recursionExpendSuggestionTreeNode(this.unitRoles, this.suggestion.unitCode, this.suggestion.roleId);
  }

  /** 节点选中事件 */
  nodeSelect(event) {
    if (this.selectedNode.data['type'] == 'U') {
      this.router.navigate(['/system/role/list', { "id": this.selectedNode['id'] }]);
    } else {
      this.router.navigate(['/system/role/info', { "id": this.selectedNode['id'] }]);
    }
  }

  /**递归展开查询的树 */
  recursionExpendSuggestionTreeNode(unitRoles: TreeNode[], roleUnitCode: string, roleId: string) {
    unitRoles.forEach(ele => {
      if(null != this.selectedNode)return;
      if (roleId == ele['id']) {
        this.selectedNode = ele;
        this.nodeSelect(null)
        return
      }
      if (roleUnitCode.indexOf(ele.data['code']) != -1) {
        //展开树节点
        ele.expanded = true;
        if (ele['data']['type'] == 'U') {
          if (ele.children && ele.children.length > 0) {
            this.recursionExpendSuggestionTreeNode(ele.children, roleUnitCode, roleId);
          } else {
            this.roleService.getUnitRoleTree(+ele["id"], false).subscribe(nodes => {
              ele.children = nodes;
              this.recursionExpendSuggestionTreeNode(ele.children, roleUnitCode, roleId);
            });
          }
        }
      } else {
        //关闭非节点
        ele.expanded = false;
      }
    })
  }

  /**递归处理树节点的变更信息 */
  recursionRefreshTreeNode(unitRoles: TreeNode[], role: any) {
    unitRoles.forEach(node => {
      if(node['id'] == role.unitId){
        this.roleService.getUnitRoleTree(role.unitId, false).subscribe(nodes => node.children = nodes);
        return;
      }
      this.recursionRefreshTreeNode(node.children, role);
    });
  }
}
